﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using DirectorioExalmarOledbHelper;
using DirectorioExalmarEntidades;
using DirectorioExalmarInterfaces;
using System.Configuration;

namespace DirectorioExalmarAccesoDatos
{
    public class Sede : ISede
    {
        #region ISede Members

        private readonly String _sCadenaConexion =
ConfigurationManager.ConnectionStrings["conDirectorioExalmar"].ConnectionString;

        public SedeInfo Consultar(SedeInfo oSede)
        {
            throw new NotImplementedException();
        }

        public IList<SedeInfo> Listar(SedeInfo oSede)
        {
            const string query = "Select Id_Sede, Descripcion from Sede";
            var oListaSede = new List<SedeInfo>();

            using (var drd = OleDbHelper.ExecuteReader(_sCadenaConexion, CommandType.Text, query))
            {
                if (drd != null)
                {
                    if (drd.HasRows)
                    {
                        while (drd.Read())
                        {
                            oListaSede.Add(new SedeInfo(drd.GetInt32(0), drd.GetString(1).Trim()));
                        }
                    }
                }
            }
            return oListaSede;
        }

        public bool Registrar(SedeInfo oSede)
        {
            var bExito = false;

            const string query = "Insert into Sede (Descripcion) Values (?)";

            var oledbParm = new OleDbParameter[1];

            using (var oledbcon = new OleDbConnection(_sCadenaConexion))
            {
                oledbcon.Open();
                var oledbtran = oledbcon.BeginTransaction(IsolationLevel.ReadCommitted);

                try
                {
                    oledbParm[0] = new OleDbParameter("@Descripcion", OleDbType.VarChar) { Value = oSede.Descripcion };

                    OleDbHelper.ExecuteNonQuery(oledbtran, CommandType.Text, query, oledbParm);
                    bExito = true;

                    if (bExito)
                    {
                        oledbtran.Commit();
                    }
                    else
                    {
                        oledbtran.Rollback();
                    }
                }
                catch
                {
                    oledbtran.Rollback();
                    bExito = false;
                }
                finally
                {
                    oledbcon.Close();
                }
            }


            return bExito;
        }

        public bool Actualizar(SedeInfo oSede)
        {
            var bExito = false;

            const string query = "Update Sede " +
                                 " Set Descripcion = ? Where Id_Sede = ?";

            var oledbParm = new OleDbParameter[2];

            using (var oledbcon = new OleDbConnection(_sCadenaConexion))
            {
                oledbcon.Open();
                var oledbtran = oledbcon.BeginTransaction(IsolationLevel.ReadCommitted);

                try
                {
                    oledbParm[0] = new OleDbParameter("@Descripcion", OleDbType.VarChar) { Value = oSede.Descripcion };
                    oledbParm[1] = new OleDbParameter("@Id_Sede", OleDbType.Integer) { Value = oSede.IdSede };

                    OleDbHelper.ExecuteNonQuery(oledbtran, CommandType.Text, query, oledbParm);
                    bExito = true;

                    if (bExito)
                    {
                        oledbtran.Commit();
                    }
                    else
                    {
                        oledbtran.Rollback();
                    }
                }
                catch
                {
                    oledbtran.Rollback();
                    bExito = false;
                }
                finally
                {
                    oledbcon.Close();
                }
            }


            return bExito;
        }

        #endregion
    }
}
